// #include"format.hpp"
// #include "level.hpp"
// #include "message.hpp"
// #include "util.hpp"
// #include"sink.hpp"
// #include "logger.hpp"
// #include "buffer.hpp"
// #include "looper.hpp"
#include "bitlog.h"
/*扩展一个以时间为滚动切换类型的日志落地模块
1.以时间段来进行滚动
实现思想：time(nullptr)%gap
例如gap=60，就是以一分钟作为一个时间段，来进行滚动切换
表示是第几个一分钟

*/
// enum class TimeGap
// {
//     GAP_SEC,
//     GAP_MIN,
//     GAP_HOUR,
//     GAP_DAY
// }
//  class RollByTimeSink:public bitlog:: logsink
//     {
//         public:
//         //构造时传入文件名，并打开文件，将操作句柄管理起来
//         RollByTimeSink(const std::string &pathname,TimeGap gaptype)
//         :_pathname(pathname)
//         {
//            switch (gaptype)
//            {
//             case TimeGap::GAP_SEC:_gap_size=1;break;
//             case TimeGap::GAP_MIN:_gap_size=60;break;
//             case TimeGap::GAP_HOUR:_gap_size=3600;break;
//             case TimeGap::GAP_DAY:_gap_size=3600*24;break;
//            }
//            _cur_gap=bitlog::util::Date::get_time()%_gap_size;
//             std::string filename = createNewFile();
//             bitlog::util::File::createDirctory(bitlog::util::File::filepath(filename);)
//             _ofs.open(filename,std::ios::binary|std::ios::app);
//             assert(_ofs.is_open());
//         }
//         //判断当前时间是否是当前文件的时间段，不是就要创建文件并且切换
//         void logdirection(const char *data,size_t len)
//         {
//             time_t cur=bitlog::util::Date::get_time();
//             if(cur%_gap_size!=_cur_gap)
//             {
//             _ofs.close();
//             std::string filename = createNewFile();
//             _ofs.open(filename,std::ios::binary|std::ios::app);
//             assert(_ofs.is_open());
//             }
//             _ofs.write(data,len);
//             assert(_ofs.good());
//         }
//         private:
//           std::string createNewFile()
//         {
//             //得到系统时间后作为文件的拓展名称
//             time_t qtime=util::Date::get_time();
//             struct tm lt;
//             localtime_r(&qtime,&lt);
//             std::stringstream filename;
//             filename<<_basename;
//             filename<<lt.tm_year+1900;
//             filename<<lt.tm_mon+1;
//             filename<<lt.tm_mday;
//             filename<<lt.tm_hour;
//             filename<<lt.tm_min;
//             filename<<lt.tm_sec;
//             filename<<"-";
//             filename<<_namecount++;
//             filename<<".log";
//             return filename.str();
//         }
//         std::string _pathname;
//         std::ofstream _ofs;
//         size_t _current_gap;
//         size_t _gap_size;
//     };
void testlog()
{
    // bitlog::logger::ptr logger= bitlog::loggermanager::getinstance().getlogger("kim_logger");
    // size_t cursize = 0,count = 0;
    // std::string str = "测试日志-";
   DEBUG("%s","测试日志");
  
}
int main()
{
//     std::ifstream ifs("./logfile/test.log",std::ios::binary);
//     if(ifs.is_open()==false)
//     {
//         std::cout<<"open failed";
//         return -1;
//     }
//     ifs.seekg(0,std::ios::end);//读写位置跳转到文件的末尾
//     size_t fsize = ifs.tellg();//获取当前位置相对于起始位置的偏移量
//     ifs.seekg(0,std::ios::beg);//重新跳转到起始位置
//     std::string body;
//     body.resize(fsize);
//     ifs.read(&body[0],fsize);
//     if(ifs.good()==false)
//     {
//         std::cout<<"read error!!!"<<std::endl;
//         return -1;
//     }
//     ifs.close();
//     bitlog::buffer mybuffer;
//     for(int i=0;i<body.size();i++)
//     {
//         mybuffer.push(&body[i],1);
//     }
//     std::ofstream ofs("./logfile/tmp.log",std::ios::binary);
//     size_t rsize = mybuffer.readsize();
//    for(int i=0;i<rsize;i++)
//    {
//     ofs.write(mybuffer.begin(),1);
//     mybuffer.moveread(1);
//    }
//    ofs.close();

    //bitlog::logmessage msg (bitlog::Level::value::INFO,53,"main.c","root","功能测试");
    //bitlog::Formatter fmt;
    //std::string str = fmt.format(msg);

    //bitlog::logsink::ptr stdout_up=bitlog::SinkFactory::create<bitlog::StdoutSink>();
   // bitlog::logsink::ptr fileout_up=bitlog::SinkFactory::create<bitlog::FileSink>("./logfile/test.log");
   // bitlog::logsink::ptr Rollout_up=bitlog::SinkFactory::create<bitlog::RollBySizeSink>("./logfile/log",1024*1024);
    // bitlog::logsink::ptr timeout_up=bitlog::SinkFactory::create<bitlog::RollTimeSink>("./logfile/logtime",TimeGap::GAP_SEC);
    // while(cursize<1024*1024*10)
    // {
    //     std::string tmp =str+std::to_string(count++);
    //     Rollout_up->logdirection(tmp.c_str(),tmp.size());
    //     cursize+=tmp.size();
    // }
    // std::string logger_name="kim_logger";
    // bitlog::Level::value limit=bitlog::Level::value::WARN;
    // bitlog::Formatter::ptr fmt(new bitlog::Formatter("[%d{%H:%M:%S}][%c][%f:%l][%p]%T%m%n"));
    // bitlog::logsink::ptr stdout_up=bitlog::SinkFactory::create<bitlog::StdoutSink>();
    // bitlog::logsink::ptr fileout_up=bitlog::SinkFactory::create<bitlog::FileSink>("./logfile/test.log");
    // bitlog::logsink::ptr Rollout_up=bitlog::SinkFactory::create<bitlog::RollBySizeSink>("./logfile/log",1024*1024);
    // std::vector<bitlog::logsink::ptr> sinks = {stdout_up,fileout_up,Rollout_up};
    // bitlog::logger::ptr logger(new bitlog::Synclogger(logger_name,limit,fmt,sinks));
    
    std::unique_ptr<bitlog::loggerbuilder> builder(new bitlog::globalloggerbuilder());//利用智能指针创建。注意格式
    builder->buildloggername("kim_logger");
    builder->buildloggertype(bitlog::loggertype::LOGGER_SYNC);
    builder->buildloggerlevel(bitlog::Level::value::DEBUG);
    builder->buildformatter("[%d{%H:%M:%S}][%t][%c][%f:%l][%p]%T%m%n");
    builder->buildsink<bitlog::FileSink>("./logfile/astest.log");
    builder->buildsink<bitlog::StdoutSink>();
    // builder->buildsink<bitlog::RollBySizeSink>("./logfile/log",1024*1024);
    builder->buildlogger();
    testlog();
    // size_t cursize = 0,count = 0;
    // std::string str = "测试日志-";
    // logger->fatal(__FILE__,__LINE__,"%s","测试日志");
    // logger->debug(__FILE__,__LINE__,"%s","测试日志");
    // logger->warn(__FILE__,__LINE__,"%s","测试日志");
    // logger->info(__FILE__,__LINE__,"%s","测试日志");
    // logger->error(__FILE__,__LINE__,"%s","测试日志");
  
}
